package com.zed.api.games;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 具有不可改变的游戏包装器,游戏管理模块统一分发主键,确保用户无法设置Id导致的主键冲突
 *
 * @author Zed
 */
public final class ImmutableGameWrapper implements Game, Serializable {
    @Serial
    private static final long serialVersionUID = -7148012343922373089L;
    private String id;
    private LocalDateTime createDate;
    private Game wrapperValue;

    /**
     * 默认构造函数,用于序列化
     */
    protected ImmutableGameWrapper() {

    }

    public ImmutableGameWrapper(String id, Game game) {
        this.id = Objects.requireNonNull(id, "\"id\" cannot be null.");
        this.wrapperValue = Objects.requireNonNull(game, "\"game\" cannot be null.");
        this.createDate = LocalDateTime.now();
    }

    /**
     * 获取未包装的原生值
     *
     * @return {@link Game}
     */
    public Game getOriginal() {
        return this.wrapperValue;
    }

    /**
     * 获取主键
     *
     * @return {@link String}
     */
    public String getId() {
        return this.id;
    }

    @Override
    public String getName() {
        return this.wrapperValue.getName();
    }

    public LocalDateTime getCreateDate() {
        return this.createDate;
    }

    @Override
    public GameLevels<?, ? extends Action> getGameLevels() {
        return this.wrapperValue.getGameLevels();
    }

    @Override
    public GameBuilder<?, ? extends Action> getGameBuilder() {
        return this.wrapperValue.getGameBuilder();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ImmutableGameWrapper that = (ImmutableGameWrapper) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
